package com.songlanyun.modules.singlegoods.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.exception.GoodsExceptionEnum;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 商家的单品表，用来管理库存
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-04 09:03:01
 */
@Data
@TableName("yxt_product_single_goods")
public class ProductSingleGoodsEntity extends RootEntity<Long, ProductSingleGoodsEntity>  implements Serializable {
	private static final long serialVersionUID = 1L;

	{
		goodsGroupName = "";
	}
	/**
	 *
	 */
	@TableId
	@NotNull(message="修改时必须指定单品Id",groups = {UpdateGroup.class})
	@Null(message = "新增时不能指定单品Id",groups = {AddGroup.class})
	private Long id;
	/**
	 * 商家店铺id
	 */
	private Long shopId;
	/**
	 * 商品分组id
	 */
	private Long goodsGroupId;


	/**
	 * 单品分组的名称
	 */
	private String goodsGroupName;
	/**
	 * 单品编码
	 */
	@NotBlank(message="单品编码不能为空",groups = {AddGroup.class,UpdateGroup.class})
	@Length(min = 1,max = 20,message = "单品编码长度在1-20个字",groups = {AddGroup.class,UpdateGroup.class})
	private String code;

	/**
	 * 单品标题
	 */
	@NotBlank(message="单品标题不能为空",groups = {AddGroup.class,UpdateGroup.class})
	@Length(min = 1,max = 100,message = "单品标题长度在1-100个字",groups = {AddGroup.class,UpdateGroup.class})
	private String title;

	/**
	 * 单品封面
	 */

	@Length(max = 500,message = "单品封面长度在500个字以内",groups = {AddGroup.class,UpdateGroup.class})
	private String coverImage;
	/**
	 * 单品规格
	 */
	@Length(max = 100,message = "单品规格长度在100个字以内",groups = {AddGroup.class,UpdateGroup.class})
	private String sku;
	/**
	 * 单品单位
	 */
	@Length(min = 1,max = 10,message = "单品单位长度在1-10个字以内",groups = {AddGroup.class,UpdateGroup.class})
	private String unit;
	/**
	 * 成本价
	 */
	@DecimalMin(value = "0",message = "成本价最小是0",groups = {AddGroup.class,UpdateGroup.class})
	@DecimalMax(value = "99999999",message = "成本价最大是99999999",groups = {AddGroup.class,UpdateGroup.class})
	private BigDecimal costPrice;
	/**
	 * 单品顺序
	 */
	@Range(min = 0, max = 1000000, message = "排序范围在0-1000000之间",groups = {AddGroup.class,UpdateGroup.class})
	private Integer orderIndex;
	/**
	 *
	 */
	@TableField(fill = FieldFill.INSERT)
	private Date createTime;
	/**
	 *
	 */
	@TableField(fill = FieldFill.INSERT_UPDATE)
	private Date updateTime;
	/**
	 * 可用库存
	 */
	@Range(min = 0, max = 99999999, message = "可用库存在0-99999999之间",groups = {AddGroup.class,UpdateGroup.class})
	private Integer ableStock;

	@Override
	public Long getIdentity() {
		return getId();
	}

	@TableLogic
	private Integer deleted;

	/**
	 * 上下架
	 */
	private Boolean added;
	/**
	 * 增加库存
	 * @param stock
	 */
	public void increaseStock(int stock)
	{
		this.setAbleStock(this.ableStock+stock);
	}

	/**
	 * 减少库存
	 * @param stock
	 */
	public void decreaseStock(int stock)
	{
		if(!isEnough(stock))
		{
			throw new RRException(String.format(GoodsExceptionEnum.SINGLE_GOODS_STOCK_SAFE.getMsg(),this.getTitle()),GoodsExceptionEnum.SINGLE_GOODS_STOCK_SAFE.getCode());
		}
		this.setAbleStock(this.ableStock-stock);
	}

	/**
	 * 库存是否足够
	 * @param stock
	 * @return
	 */
	public boolean isEnough(int stock)
	{
		return this.ableStock >= stock;
	}

}
